﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using RTree;
using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace SkyPoint
{
    public class FixedSkyCatalogue
    {
        RTree<SkyObject> catalogue;
        CSVLoader csvLoader;

        public FixedSkyCatalogue()
        {
            catalogue = new RTree<SkyObject>();
            csvLoader = new CSVLoader();
            List<SkyObject> skyObjects = csvLoader.ExtractStars();
            foreach (SkyObject skyObject in skyObjects)
            {
                Vector3 vector = Utils.ConvertSphericalToCartesian(skyObject.Coordinates);
                float [] mins = new float[] {vector.X,vector.Y,vector.Z};
                RTree.Rectangle rect = new RTree.Rectangle(mins,mins);
                catalogue.Add(rect, skyObject);
            }
        }


        public List<SkyObject> NearbyStars(Coord coordinate)
        {
            SkyComparator comp = new SkyComparator(coordinate);

            List<SkyObject> ret = catalogue.Intersects(generateRectangle(coordinate));
            ret.Sort(comp.Compare);
            return ret;
            
        }

        private RTree.Rectangle generateRectangle(Coord coordinate)
        {
            
            float c = 0.2F;
            Vector3 vector = Utils.ConvertSphericalToCartesian(coordinate);
            float[] mins = new float[] { vector.X - c, vector.Y - c, vector.Z - c };
            float[] maxs = new float[] { vector.X + c, vector.Y + c, vector.Z + c };
            return new RTree.Rectangle(mins, maxs);
            
        }

        

    }
}
